package com.mj;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

import javax.swing.text.rtf.RTFEditorKit;
import javax.swing.tree.TreeNode;

import com.mj.BinaryTreeLeetCode.Node;
import com.mj.printer.BinaryTreeInfo;

@SuppressWarnings({ "unused", "rawtypes" })
public class BinarySearchTreeLeetCode extends BinaryTreeLeetCode  {

	@SuppressWarnings("null")
	public void add(Integer element) {
		if (root == null) {
			root = new Node(element, null);
			size++;
			return;
		}
		Node node = root;
		Node parent = null;
		int cmp = 0;
		while (node != null) {
			cmp = element - node.element;
			parent = node;
			if (cmp > 0) {
				node = node.right;
			}else if (cmp < 0) {
				node = node.left;
			}else {
				return;
			}
		}
		Node newNode = new Node(element, parent);
		if (cmp > 0) {
			parent.right = newNode;
		}else {
			parent.left = newNode;
		}
		size++;
	}
	
	public boolean isValidBST() {
		Deque<Node> stack = new LinkedList<TreeNode>();
		while (!stack.isEmpty() || root != null) {
			while (root != null) {
				stack.push( root);
				root = root.left;
			}
			
		}
		return true;
	}

	public Node node(Integer element) {
		Node node = root;
		while (node != null) {
			int cmp = element - node.element;
			if (cmp == 0) {
				return node;
			}else if (cmp > 0) {
				node = node.right;
			}else {
				node = node.left;
			}
		}
		return null;
	}
}
